package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.ttn.business.service.OrderService;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.Order;
import com.ttn.common.core.entity.business.query.OrderPageQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (order)表控制层
 *
 * @author xxxxx
 */
@RestController
@Slf4j
@RequestMapping("/order")
public class OrderController {
    /**
     * 服务对象
     */
    @Resource
    private OrderService orderService;
    @Resource
    private WxPayService wxPayService;

    /**
     * 保存订单
     *
     * @param request
     * @param order
     * @return
     */
    @PostMapping("/save/order")
    public Result saveOrder(HttpServletRequest request, @RequestBody Order order) {
        log.info("OrderController saveOrder ,参数:{}", order);
        return orderService.saveOrder(order);
    }

    /**
     * 此处处理订单信息，构建订单数据。
     * <p>
     * 将构建好的支付参数返回到前端，前端调起微信支付
     *
     * @return
     */

    @PostMapping("/save/order-by-student")
    public Result saveOrderByStudent(HttpServletRequest request, @RequestBody Order order) {
        log.info("OrderController saveOrderByStudent ,参数:{}", order);
        return orderService.saveOrderByStudent(order, request);
    }

    /**
     * @param xmlData 微信返回的流
     * @return
     */
    @RequestMapping(value = "/weChatPayNotify", method = {RequestMethod.GET, RequestMethod.POST})
    public String weChatNotify(@RequestBody String xmlData) {

        try {
            log.info("xmlData");
            final WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
            //这里是存储我们发起支付时订单号的信息，所以取出来

            orderService.paymentOrderSuccessful(notifyResult.getOutTradeNo());
            //成功后回调微信信息
            return WxPayNotifyResponse.success("回调成功！");
        } catch (WxPayException e) {
            e.printStackTrace();
            return WxPayNotifyResponse.fail("回调有误!");
        }
    }

    /**
     * 查询订单分页
     *
     * @param request
     * @param orderPageQuery
     * @return
     */
    @PostMapping("/query/order-page")
    public Result queryOrderPage(HttpServletRequest request, @RequestBody OrderPageQuery orderPageQuery) {
        log.info("OrderController orderQuery ,参数:{}", orderPageQuery);
        return orderService.queryOrderPage(orderPageQuery);
    }

    /**
     * 通过订单编号查询订单详情
     *
     * @param request
     * @param orderNumber
     * @return
     */
    @PostMapping("/query/order-details-by-order-number/{orderNumber}")
    public Result queryOrderDetailsByOrderNumber(HttpServletRequest request, @PathVariable String orderNumber) {
        log.info("OrderController queryOrderDetailsByOrderNumber ,参数:{}", orderNumber);
        return orderService.queryOrderDetailsByOrderNumber(orderNumber);
    }


    /**
     * 通过学员id，获取订单
     *
     * @param request
     * @param studentId
     * @return
     */
    @PostMapping("/query/order-details-by-student_id/{studentId}")
    public Result serviceQueryOrderListByStudentId(@PathVariable String studentId, HttpServletRequest request) {
        log.info("OrderController serviceQueryOrderListByStudentId ,参数:{}", studentId);
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Order::getStudentId, studentId);
        orderLambdaQueryWrapper.orderByDesc(Order::getPaymentTime);
        return Result.success(orderService.list(orderLambdaQueryWrapper));
    }

    @PostMapping("/query/service-order-details-by-student-id/{studentId}")
    public Result queryServiceOrderDetailsByStudentId(@PathVariable Integer studentId) {
        log.info("OrderController queryServiceOrderDetailsByStudentId ,参数:{}", studentId);
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Order::getStudentId, studentId);
        orderLambdaQueryWrapper.orderByDesc(Order::getPaymentTime);
        orderLambdaQueryWrapper.eq(Order::getOrderStatus, 1);
        orderLambdaQueryWrapper.last(" limit 1");
        return Result.success(orderService.getOne(orderLambdaQueryWrapper));
    }

    @GetMapping("/query/order-statistics")
    public Result queryOrderStatistics() {
        return orderService.queryOrderStatistics();
    }


}
